package com.tianyu.service.mqtt.common;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;


/**
 * @author swyan
 */

@Data
@Configuration
@ConfigurationProperties(prefix = "app-constants.thread-pool-config")
public class MqttThreadPoolConfig {

    private int corePoolSize;

    private int maximumPoolSize;

    private long keepAliveTime;

    private int capacity;

    /**
     * A custom thread pool.
     * @return
     */
    @Bean
    public Executor mqttThreadPool() {
        return new ThreadPoolExecutor(corePoolSize,
                maximumPoolSize, keepAliveTime,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(capacity),
                Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
    }

}
